Letter to digit


In [1]:
print("ACGT".index("C"))
d={"A":0,"C":1,"G":2,"T":3}
print(d)


1
{'A': 0, 'C': 1, 'G': 2, 'T': 3}

In [2]:
d2={}
for i,l in enumerate("ACGT"):
    print(i, l)
    d2[l]=i
print(d2)


0 A
1 C
2 G
3 T
{'A': 0, 'C': 1, 'G': 2, 'T': 3}

In [3]:
#dict([("A",0),("C",1)...])
d3=dict( ((l,i) for i,l in enumerate("ACGT")) )
print(d3)


{'A': 0, 'C': 1, 'G': 2, 'T': 3}

Power operator


In [4]:
print(4**3)


64

In [5]:
for p,d in enumerate("ACGTCAGAC"[::-1]):
    print("Digit",d,"power",p)


Digit C power 0
Digit A power 1
Digit G power 2
Digit A power 3
Digit C power 4
Digit T power 5
Digit G power 6
Digit C power 7
Digit A power 8

In [6]:
print([0]*22)

d1={}
d2=d1

#Careful! This does not create 22 dictionaries, it creates a list
#with 22 pointers to the exact same dictionary
lst=[{}]*22
print(lst)

lst[0]["k"]="v" #All of them will change!
print(lst)

#This is a way to create 22 dictionaries
lst=[{} for _ in range(22)]
print(lst)
lst[0]["k"]="v"
print(lst)


[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
[{'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}, {'k': 'v'}]
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
[{'k': 'v'}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

In [7]:
print([[0]*10 for _ in range(10)])


[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

In [8]:
#Just a reminder, nothing else
spectrum=[0]*64
spectrum[5]=54656
spectrum[5]+=1

In [9]:
counter={}
for l in "hfjdskohgfjlkdsahfjlsahfjlksa":
    counter[l]=counter.get(l,0)+1
print(counter)


{'h': 4, 'f': 4, 'j': 4, 'd': 2, 's': 4, 'k': 3, 'o': 1, 'g': 1, 'l': 3, 'a': 3}

In [ ]: